{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline \n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ejercicio: Paseo aleatorio\n",
    "En el paseo aleatorio, se parte de un valor inicial $V_0$. Se lanza una moneda equilibrada. Si sale cara, sumamos 1 al valor ($V_1 = V_0 + 1$). Si sale cruz, se lo restamos. Este proceso se repite hasta que se alcanza alguno de los valores del conjunto $B$ (bordes).\n",
    "\n",
    "Lo que se pide es implementar la funcion `caminoAleatorio(v0, bordes)`, que devuelva la lista de valores hasta llegar a un borde."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import random\n",
    "def caminoAleatorio(v0, bordes):\n",
    "    recorrido = [v0]\n",
    "    while v0 not in bordes:\n",
    "        v0 += random.choice([-1, 1])\n",
    "        recorrido.append(v0)\n",
    "    return recorrido"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ahora que la tenemos, obtenemos un camino para $v_0=3$ y $B=\\{0, 10\\}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Ejemplo de ejecucion\n",
    "bordes = [0, 20]\n",
    "camino = caminoAleatorio(8, bordes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1, 1)\n",
    "ax.plot(camino)\n",
    "ax.set_xlabel('Pasos')\n",
    "ax.set_ylabel('Nivel')\n",
    "ax.set_title('Evolución de un camino aleatorio');\n",
    "for bor in bordes:\n",
    "    ax.plot([0, len(camino)-1], [bor, bor], color='red', lw=1, linestyle='dashdot', alpha=0.6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
